package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.RecordArchive;
import com.ruoyi.system.domain.RecordArchiveset;
import com.ruoyi.system.mapper.RecordArchiveMapper;
import com.ruoyi.system.service.IRecordArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 案卷信息Service业务层处理
 *
 * @author admin
 * @date 2022-02-21
 */
@Service
public class RecordArchiveServiceImpl implements IRecordArchiveService {
    @Autowired
    private RecordArchiveMapper recordArchiveMapper;

    /**
     * 查询案卷信息
     *
     * @param archiveId 案卷信息主键
     * @return 案卷信息
     */
    @Override
    public RecordArchive selectRecordArchiveByArchiveId(Long archiveId) {
        return recordArchiveMapper.selectRecordArchiveByArchiveId(archiveId);
    }

    /**
     * 查询案卷信息列表
     *
     * @param recordArchive 案卷信息
     * @return 案卷信息
     */
    @Override
    public List<RecordArchive> selectRecordArchiveList(RecordArchive recordArchive) {
        return recordArchiveMapper.selectRecordArchiveList(recordArchive);
    }

    /**
     * 新增案卷信息
     *
     * @param recordArchive 案卷信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRecordArchive(RecordArchive recordArchive) {
        int rows = recordArchiveMapper.insertRecordArchive(recordArchive);
        insertRecordArchiveset(recordArchive);
        return rows;
    }

    /**
     * 修改案卷信息
     *
     * @param recordArchive 案卷信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRecordArchive(RecordArchive recordArchive) {
        recordArchiveMapper.deleteRecordArchivesetByArchiveId(recordArchive.getArchiveId());
        insertRecordArchiveset(recordArchive);
        return recordArchiveMapper.updateRecordArchive(recordArchive);
    }

    /**
     * 批量删除案卷信息
     *
     * @param archiveIds 需要删除的案卷信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordArchiveByArchiveIds(Long[] archiveIds) {
        recordArchiveMapper.deleteRecordArchivesetByArchiveIds(archiveIds);
        return recordArchiveMapper.deleteRecordArchiveByArchiveIds(archiveIds);
    }

    /**
     * 删除案卷信息信息
     *
     * @param archiveId 案卷信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordArchiveByArchiveId(Long archiveId) {
        recordArchiveMapper.deleteRecordArchivesetByArchiveId(archiveId);
        return recordArchiveMapper.deleteRecordArchiveByArchiveId(archiveId);
    }

    /**
     * 新增案卷目录配置信息
     *
     * @param recordArchive 案卷信息对象
     */
    public void insertRecordArchiveset(RecordArchive recordArchive) {
        List<RecordArchiveset> recordArchivesetList = recordArchive.getRecordArchivesetList();
        Long archiveId = recordArchive.getArchiveId();
        if (StringUtils.isNotNull(recordArchivesetList)) {
            List<RecordArchiveset> list = new ArrayList<RecordArchiveset>();
            for (RecordArchiveset recordArchiveset : recordArchivesetList) {
                recordArchiveset.setArchiveId(archiveId);
                list.add(recordArchiveset);
            }
            if (list.size() > 0) {
                recordArchiveMapper.batchRecordArchiveset(list);
            }
        }
    }
}
